package com.itmq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class ConsumerDemo {
    public static void main(String[] args) throws Exception {
        //创建连接工厂
        ActiveMQConnectionFactory connectionFactory=new ActiveMQConnectionFactory("tcp://192.168.101.128:61616");
        //通过工厂获取连接，并开启连接
        Connection connection = connectionFactory.createConnection();
        connection.start();
        //通过连接获取会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //创建目的地
       // Queue queue = session.createQueue("queue_107");//队列
        Topic topic = session.createTopic("topic_107");//主题
        //创建消费者
        MessageConsumer consumer = session.createConsumer(topic);
        //监听中间件获取消息
        consumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                //如果activemq中有消息，这个方法就会执行，参数就是获取到的消息
                try {
                    String text=((TextMessage) message).getText();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });



    }


}
